/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package cn.apestar.modules.report.paymentcostlog.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import cn.apestar.common.config.Global;
import cn.apestar.common.persistence.Page;
import cn.apestar.common.web.BaseController;
import cn.apestar.modules.report.paymentcostlog.entity.PaymentCostLog;
import cn.apestar.modules.report.paymentcostlog.service.PaymentCostLogService;

/**
 * 现金支付日志Controller
 * @author yudw
 * @version 2016-09-27
 */
@Controller
@RequestMapping(value = "${adminPath}/paymentcostlog/paymentCostLog")
public class PaymentCostLogController extends BaseController {

    @Autowired
    private PaymentCostLogService paymentCostLogService;

    @ModelAttribute
    public PaymentCostLog get(@RequestParam(required = false) String id) {
        PaymentCostLog entity = null;
        if (StringUtils.isNotBlank(id)) {
            entity = paymentCostLogService.get(id);
        }
        if (entity == null) {
            entity = new PaymentCostLog();
        }
        return entity;
    }

    @RequestMapping(value = {"list" , ""})
    public String list(PaymentCostLog paymentCostLog, HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<PaymentCostLog> page = paymentCostLogService.findPage(new Page<PaymentCostLog>(request, response), paymentCostLog);
        model.addAttribute("page", page);
        model.addAttribute("paymentCostLog", paymentCostLog);
        model.addAttribute("bizType", paymentCostLog.getBizType());
        model.addAttribute("sum", paymentCostLogService.sumMoney(paymentCostLog));
        return "modules/report/paymentcostlog/paymentCostLogList";
    }

    @RequestMapping(value = "form")
    public String form(PaymentCostLog paymentCostLog, Model model) {
        model.addAttribute("paymentCostLog", paymentCostLog);
        return "modules/report/paymentcostlog/paymentCostLogForm";
    }

    @RequestMapping(value = "save")
    public String save(PaymentCostLog paymentCostLog, Model model, RedirectAttributes redirectAttributes) {
        if (!beanValidator(model, paymentCostLog)) {
            return form(paymentCostLog, model);
        }
        paymentCostLogService.save(paymentCostLog);
        addMessage(redirectAttributes, "保存现金支付日志成功");
        return "redirect:" + Global.getAdminPath() + "/paymentcostlog/paymentCostLog/?repage";
    }

    @RequestMapping(value = "delete")
    public String delete(PaymentCostLog paymentCostLog, RedirectAttributes redirectAttributes) {
        paymentCostLogService.delete(paymentCostLog);
        addMessage(redirectAttributes, "删除现金支付日志成功");
        return "redirect:" + Global.getAdminPath() + "/paymentcostlog/paymentCostLog/?repage";
    }

}